package com.shuoyue.ycgk.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.utils.GridSpacingItemDecoration;
import com.shuoyue.ycgk.utils.SizeUtil;

import java.util.ArrayList;
import java.util.List;

public class AnswerCardDialog extends Dialog {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private TextView useTime;
    LinearLayout layUseTime;
    ImageButton close;
    Button submit;

    private ExaminationAnswerCardAdapter mAdapter;

    public interface AnswerCardCallBack {
        void select(QuestionSimple questionSimple);

        void submit();
    }

    public AnswerCardDialog(@NonNull Context context, List<QuestionParent> mDatas) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setWindowAnimations(R.style.ChooseStyle);
        setContentView(R.layout.dialog_answer_card);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setGravity(Gravity.BOTTOM);

        mRecyclerView = findViewById(R.id.recycler_view);
        useTime = findViewById(R.id.use_time);
        close = findViewById(R.id.close);
        submit = findViewById(R.id.submit);
        layUseTime = findViewById(R.id.layUseTime);

        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(5, SizeUtil.dip2px(mContext, 20), false));

        mAdapter = new ExaminationAnswerCardAdapter(allSimple(mDatas));
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.numtext:
                    if (call != null) {
                        QuestionSimple questionSimple = mAdapter.getItem(position);
                        call.select(questionSimple);
                    }
                    hide();
                    break;
            }
        });

        submit.setOnClickListener(v -> {
            if (call != null) {
                call.submit();
            }
            hide();
        });
        close.setOnClickListener(v -> hide());


    }

    AnswerCardCallBack call;

    public void addSelectCall(AnswerCardCallBack call) {
        this.call = call;
    }

    public void setButtonText(String buttonText) {
        this.submit.setText(buttonText);// = call;
    }

    /**
     * 提取基础题目
     */
    private List<QuestionSimple> allSimple(List<QuestionParent> questionParents) {

        List<QuestionSimple> allSimple = new ArrayList<>();
        if (questionParents == null) {
            return allSimple;
        }
        for (QuestionParent questionParent : questionParents) {
            //
            if (questionParent.isSpcePage()) {
                continue;
            }
            if (questionParent.getIsMaterial() == 1) {

                //材料题
                for (QuestionSimple questionSimple : questionParent.getQuestionSimpleDTOS()) {
                    allSimple.add(questionSimple);
                }
            } else {
                allSimple.add(questionParent.getQuestionSimpleDTOS().get(0));
            }
        }
        return allSimple;
    }

    public void setUseTime(String text) {
        layUseTime.setVisibility(View.VISIBLE);
        useTime.setText(text);
    }

    @Override
    public void show() {
        super.show();
        mAdapter.notifyDataSetChanged();
    }


    class ExaminationAnswerCardAdapter extends AppBaseQuickAdapter<QuestionSimple> {
        public ExaminationAnswerCardAdapter(@Nullable List<QuestionSimple> data) {
            super(R.layout.item_answer_card, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, QuestionSimple item) {
            helper.addOnClickListener(R.id.numtext);
            helper.setText(R.id.numtext, item.getNum() + "");
            if (item.getIsAnswerRight() == 1 || item.getIsAnswerRight() == 0) {
            } else {
                helper.setTextColor(R.id.numtext, ContextCompat.getColor(mContext, R.color.text_sub_title));
            }
            helper.setTextColor(R.id.numtext,
                    item.getIsAnswerRight() == 2 ?
                            ContextCompat.getColor(mContext, R.color.all_text_gray_color)
                            : ContextCompat.getColor(mContext, R.color.option_bg_blue));
            helper.getView(R.id.numtext).setBackground(
                    item.getIsAnswerRight() == 2 ?
                            ContextCompat.getDrawable(mContext, R.drawable.bg_answer_card_item_unselect) :
                            ContextCompat.getDrawable(mContext, R.drawable.bg_answer_card_item_selected));

        }
    }
}
